package com.cian.service.impl;

import com.cian.entity.History;
import com.cian.entity.dto.AccountHistoryDTO;
import com.cian.entity.dto.GoodsHistoryDTO;
import com.cian.mapper.HistoryMapper;
import com.cian.service.IHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 销售记录的历史表项 服务实现类
 * </p>
 *
 * @author Cian
 * @since 2021-03-15
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements IHistoryService {
    @Autowired
    HistoryMapper historyMapper;

    @Override
    public boolean changeStock(String account, String serverCode, Date date, int coId, int stock) {
        try{
            // 需要判断是否存在历史，如果不存在则进行添加，存在则进行更新
            History history = getHistory(account, serverCode, date, coId);
            if (history == null){
                // 表示无缓存记录，需要进行新建
                historyMapper.insertChangeStock(account, serverCode, new java.sql.Date(date.getTime()), coId, stock);
            } else {
                // 表示存在缓存记录，需要进行更新
                historyMapper.setSalfValue(0);
                historyMapper.updateChangeStock(account, serverCode, new java.sql.Date(date.getTime()), coId, stock);
                historyMapper.setSalfValue(1);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean changeInput(String account, String serverCode, Date date, int coId, int input) {
        try{
            // 需要判断是否存在历史，如果不存在则进行添加，存在则进行更新
            // 需要判断是否存在历史，如果不存在则进行添加，存在则进行更新
            History history = getHistory(account, serverCode, date, coId);
            if (history == null){
                // 表示无缓存记录，需要进行新建
                historyMapper.insertInputStock(account, serverCode, new java.sql.Date(date.getTime()), coId, input);
            } else {
                // 表示存在缓存记录，需要进行更新
                historyMapper.setSalfValue(0);
                historyMapper.updateInputStock(account, serverCode, new java.sql.Date(date.getTime()), coId, input);
                historyMapper.setSalfValue(1);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public History getHistory(String account, String serverCode, Date date, int coId) {
        Map<String,Object> map = new HashMap<>();
        map.put("account", account);
        map.put("serve_code", serverCode);
        map.put("co_id", coId);
        map.put("create_time", new java.sql.Date(date.getTime()));
        List<History> histories = historyMapper.selectByMap(map);
        if(histories == null || histories.isEmpty()) return null;
        return histories.get(0);
    }

    @Override
    public AccountHistoryDTO obtainAccountHistory(String account, Date date) {
        AccountHistoryDTO accountHistoryDTO =  historyMapper.obtainAccountHistory(account,
                new java.sql.Date(date.getTime()));
        // 判断是否有找到，如果没有找到，则返回一个内容全为0的对象
        if(accountHistoryDTO == null){
            return new AccountHistoryDTO(date,0, 0.0,0,0);
        } else {
            return accountHistoryDTO;
        }
    }

    @Override
    public AccountHistoryDTO obtainServerHistoryForLine(String account, Date date, String serverCode){
        AccountHistoryDTO accountHistoryDTO =  historyMapper.obtainHistoryForLine(account, serverCode,
                new java.sql.Date(date.getTime()));
        // 判断是否有找到，如果没有找到，则返回一个内容全为0的对象
        if(accountHistoryDTO == null){
            return new AccountHistoryDTO(date,0, 0.0,0,0);
        } else {
            return accountHistoryDTO;
        }
    }

    @Override
    public List<GoodsHistoryDTO> obtainGoodsHistoryForLine(String account, String serverCode,
                                                     Date leftDate, Date rightDate) {
        List<GoodsHistoryDTO> goodsHistoryDTO = historyMapper.obtainGoodsHistory(account, serverCode,
                new java.sql.Date(leftDate.getTime()), new java.sql.Date(rightDate.getTime()));
        // 判断是否有找到，如果没有找到，则返回一个内容全为0的对象
        if(goodsHistoryDTO == null){
            goodsHistoryDTO = (new LinkedList<GoodsHistoryDTO>());
            goodsHistoryDTO.add(new GoodsHistoryDTO("",0.0));
            return goodsHistoryDTO;
        } else {
            return goodsHistoryDTO;
        }
    }

    @Override
    public List<GoodsHistoryDTO> obtainPriceHistoryForLine(String account, String serverCode,
                                                     Date leftDate, Date rightDate) {
        List<GoodsHistoryDTO> goodsHistoryDTO = historyMapper.obtainPriceHistory(account, serverCode,
                new java.sql.Date(leftDate.getTime()), new java.sql.Date(rightDate.getTime()));
        // 判断是否有找到，如果没有找到，则返回一个内容全为0的对象
        if(goodsHistoryDTO == null){
            goodsHistoryDTO = (new LinkedList<GoodsHistoryDTO>());
            goodsHistoryDTO.add(new GoodsHistoryDTO("",0.0));
            return goodsHistoryDTO;
        } else {
            return goodsHistoryDTO;
        }
    }
}
